package com.exchange.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.exchange.entity.pojo.CardDisplay;
import com.exchange.entity.pojo.CardRoll;
import com.exchange.entity.pojo.User;
import com.exchange.entity.vo.CardDisplayVo;
import com.exchange.mapper.CardDisplayMapper;
import com.exchange.mapper.CardRollMapper;
import com.exchange.mapper.UserMapper;
import com.exchange.service.CardRollService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

/**
* @author Administrator
* @description 针对表【card_roll】的数据库操作Service实现
* @createDate 2024-04-25 20:02:10
*/
@Service
public class CardRollServiceImpl extends ServiceImpl<CardRollMapper, CardRoll>
    implements CardRollService{
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CardRollMapper cardRollMapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private CardDisplayMapper cardDisplayMapper;



    @Override
    public List<CardDisplayVo> MyCardListByType(String token, Integer type) {
        String userinfo = this.redisTemplate.opsForValue().get("user:login" + token);
        User user = JSON.parseObject(userinfo, User.class);
        return this.cardRollMapper.MyCardListByType(user.getId(), type);

    }
    @Override
    public List<CardDisplayVo> findAll(String token) {
        String userinfo = this.redisTemplate.opsForValue().get("user:login" + token);
        System.err.println("userinfo：" + userinfo);
        User user = JSON.parseObject(userinfo, User.class);
        BigInteger bigInteger = this.userMapper.selectUid(user.getMobile());
        return this.cardRollMapper.findAll(bigInteger);
    }

    @Override
    public void cardRoll(CardDisplay cardDisplay, String token) {
        String users = this.redisTemplate.opsForValue().get("user:login"+token);
        System.err.println("userinfo：" + users);
        User user = JSON.parseObject(users, User.class);
        Long uid = user.getId();
        String mobile = user.getMobile();

        Long id = cardDisplay.getId();
        QueryWrapper<CardDisplay> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        CardDisplay cardDisplay1 = this.cardDisplayMapper.selectOne(queryWrapper);
        System.err.println("cardDisplay1：cardDisplay1：" + cardDisplay1);
        String cardName = cardDisplay1.getCardName();
        BigDecimal countPrice = cardDisplay1.getCountPrice();
        this.cardRollMapper.cardRoll(id, cardName, countPrice, uid);
    }
}




